package com.gitee.wsl.collections.queue

import com.gitee.wsl.collections.api.BoundedCollection
import com.gitee.wsl.ext.base.Arrays.arraycopy
import com.gitee.wsl.text.format.format
import kotlin.jvm.JvmOverloads
import kotlin.jvm.Transient


/**
 * CircularFifoQueue is a first-in first-out queue with a fixed size that
 * replaces its oldest element if full.
 *
 *
 * The removal order of a [CircularFifoQueue] is based on the
 * insertion order; elements are removed in the same order in which they
 * were added.  The iteration order is the same as the removal order.
 *
 *
 *
 * The [.add], [.remove], [.peek], [.poll],
 * [.offer] operations all perform in constant time.
 * All other operations perform in linear time or worse.
 *
 *
 *
 * This queue prevents null objects from being added.
 *
 *
 * @param <E> the type of elements in this collection
 * @since 4.0
</E> */
class CircularFifoQueue<E> @JvmOverloads constructor(
    size: Int = 32
) : AbstractCollection<E>(), Queue<E>, BoundedCollection<E> {
    /** Underlying storage array.  */
    @Transient
    private var elements: Array<E?>

    /** Array index of first (oldest) queue element.  */
    @Transient
    private var start = 0

    /**
     * Index mod maxElements of the array position following the last queue
     * element.  Queue elements start at elements[start] and "wrap around"
     * elements[maxElements-1], ending at elements[decrement(end)].
     * For example, elements = {c,a,b}, start=1, end=1 corresponds to
     * the queue [a,b,c].
     */
    @Transient
    private var end = 0

    /** Flag to indicate if the queue is currently full.  */
    @Transient
    private var full = false

    /** Capacity of the queue.  */
    private val maxElements: Int

    /**
     * Constructor that creates a queue from the specified collection.
     * The collection size also sets the queue size.
     *
     * @param coll  the collection to copy into the queue, may not be null
     * @throws NullPointerException if the collection is null
     */
    constructor(coll: Collection< E>) : this(coll.size) {
        addAll(coll)
    }

    /**
     * Constructor that creates a queue with the specified size.
     *
     * @param size  the size of the queue (cannot be changed)
     * @throws IllegalArgumentException  if the size is &lt; 1
     */
    /**
     * Constructor that creates a queue with the default size of 32.
     */
    init {
        require(size > 0) { "The size must be greater than 0" }
        elements = kotlin.arrayOfNulls<Any>(size) as Array<E?>
        maxElements = elements.size
    }

    /**
     * Adds the given element to this queue. If the queue is full, the least recently added
     * element is discarded so that a new element can be inserted.
     *
     * @param element  the element to add
     * @return true, always
     * @throws NullPointerException  if the given element is null
     */
    override fun add(element: E): Boolean {

        if (this.isAtFullCapacity) {
            remove()
        }

        elements[end++] = element

        if (end >= maxElements) {
            end = 0
        }

        if (end == start) {
            full = true
        }

        return true
    }

    /**
     * Clears this queue.
     */
    override fun clear() {
        full = false
        start = 0
        end = 0
        elements.fill(null)
    }

    /**
     * Decrements the internal index.
     *
     * @param index  the index to decrement
     * @return the updated index
     */
    private fun decrement(index: Int): Int {
        var index = index
        index--
        if (index < 0) {
            index = maxElements - 1
        }
        return index
    }

    override fun element(): E {
        if (isEmpty()) {
            throw NoSuchElementException("queue is empty")
        }
        return peek()
    }

    /**
     * Returns the element at the specified position in this queue.
     *
     * @param index the position of the element in the queue
     * @return the element at position `index`
     * @throws NoSuchElementException if the requested position is outside the range [0, size)
     */
    fun get(index: Int): E? {
        val sz = size
        if (index < 0 || index >= sz) {
            throw NoSuchElementException(
                String.format(
                    "The specified index %1\$d is outside the available range [0, %2\$d)",
                    index, sz
                )
            )
        }

        val idx = (start + index) % maxElements
        return elements[idx]
    }

    /**
     * Increments the internal index.
     *
     * @param index  the index to increment
     * @return the updated index
     */
    private fun increment(index: Int): Int {
        var index = index
        index++
        if (index >= maxElements) {
            index = 0
        }
        return index
    }

    val isAtFullCapacity: Boolean
        /**
         * Returns `true` if the capacity limit of this queue has been reached,
         * i.e. the number of elements stored in the queue equals its maximum size.
         *
         * @return `true` if the capacity limit has been reached, `false` otherwise
         * @since 4.1
         */
        get() = size == maxElements

    /**
     * Returns true if this queue is empty; false otherwise.
     *
     * @return true if this queue is empty
     */
    public override fun isEmpty(): Boolean {
        return size == 0
    }

    /**
     * {@inheritDoc}
     *
     *
     * A `CircularFifoQueue` can never be full, thus this returns always
     * `false`.
     *
     * @return always returns `false`
     */
    override val isFull: Boolean
        get() = false

    /**
     * Returns an iterator over this queue's elements.
     *
     * @return an iterator over this queue's elements
     */
    public override fun iterator(): MutableIterator<E> {
        return object : MutableIterator<E> {
            private var index = start
            private var lastReturnedIndex = -1
            private var isFirst = full

            public override fun hasNext(): Boolean {
                return isFirst || index != end
            }

            public override fun next(): E {
                if (!hasNext()) {
                    throw NoSuchElementException()
                }
                isFirst = false
                lastReturnedIndex = index
                index = increment(index)
                return elements[lastReturnedIndex]!!
            }

            override fun remove() {
                check(lastReturnedIndex != -1)

                // First element can be removed quickly
                if (lastReturnedIndex == start) {
                    this@CircularFifoQueue.remove()
                    lastReturnedIndex = -1
                    return
                }

                var pos = lastReturnedIndex + 1
                if (start < lastReturnedIndex && pos < end) {
                    // shift in one part
                    arraycopy(elements, pos, elements, lastReturnedIndex, end - pos)
                } else {
                    // Other elements require us to shift the subsequent elements
                    while (pos != end) {
                        if (pos >= maxElements) {
                            elements[pos - 1] = elements[0]
                            pos = 0
                        } else {
                            elements[decrement(pos)] = elements[pos]
                            pos = increment(pos)
                        }
                    }
                }

                lastReturnedIndex = -1
                end = decrement(end)
                elements[end] = null
                full = false
                index = decrement(index)
            }
        }
    }

    override fun containsAll(elements: Collection<E>): Boolean = super<AbstractCollection>.containsAll(elements)

    /**
     * Gets the maximum size of the collection (the bound).
     *
     * @return the maximum number of elements the collection can hold
     */
    override val maxSize: Int
        get() = maxElements

    /**
     * Adds the given element to this queue. If the queue is full, the least recently added
     * element is discarded so that a new element can be inserted.
     *
     * @param e  the element to add
     * @return true, always
     * @throws NullPointerException  if the given element is null
     */
    override fun offer(e: E): Boolean {
        return add(e)
    }

    override fun peekOrNull(): E? {
        if (isEmpty()) {
            return null
        }
        return elements[start]
    }

    override fun pollOrNull(): E? {
        if (isEmpty()) {
            return null
        }
        return remove()
    }

    /**
     * Deserializes the queue in using a custom routine.
     *
     * @param in  the input stream
     * @throws IOException if an I/O error occurs while writing to the output stream
     * @throws ClassNotFoundException if the class of a serialized object cannot be found
     */
//    @Throws(IOException::class, java.lang.ClassNotFoundException::class)
//    private fun readObject(`in`: ObjectInputStream) {
//        `in`.defaultReadObject()
//        elements = kotlin.arrayOfNulls<Any>(maxElements) as Array<E?>
//        val size: Int = `in`.readInt()
//        for (i in 0..<size) {
//            elements[i] = `in`.readObject() as E?
//        }
//        start = 0
//        full = size == maxElements
//        if (full) {
//            end = 0
//        } else {
//            end = size
//        }
//    }

    override fun remove(): E? {
        if (isEmpty()) {
            throw NoSuchElementException("queue is empty")
        }

        val element = elements[start]
        if (null != element) {
            elements[start++] = null

            if (start >= maxElements) {
                start = 0
            }
            full = false
        }
        return element
    }

    override fun remove(element: E): Boolean {
        TODO("Not yet implemented")
    }

    /**
     * Returns the number of elements stored in the queue.
     *
     * @return this queue's size
     */
    override val size: Int
        get() = _size()

    private  fun _size(): Int {
        var size = 0

        if (end < start) {
            size = maxElements - start + end
        } else if (end == start) {
            size = if (full) maxElements else 0
        } else {
            size = end - start
        }

        return size
    }

    /**
     * Serializes this object to an ObjectOutputStream.
     *
     * @param out the target ObjectOutputStream.
     * @throws IOException thrown when an I/O errors occur writing to the target stream.
     */
//    @Throws(IOException::class)
//    private fun writeObject(out: ObjectOutputStream) {
//        out.defaultWriteObject()
//        out.writeInt(size())
//        for (e in this) {
//            out.writeObject(e)
//        }
//   }

}
